package practise;

import java.util.Stack;

public class IsPopOrder {
    public static boolean IsPopOrder (int[] pushV, int[] popV) {
        Stack<Integer> stack = new Stack<>();
        int indexPush = 0;
        int indexPop = 0;
        while(indexPush < pushV.length) {
            if(stack.isEmpty() || !stack.peek().equals(popV[indexPop])) {
                stack.push(pushV[indexPush++]);
            }
            while(!stack.isEmpty() && stack.peek().equals(popV[indexPop])) {
                stack.pop();
                indexPop++;
            }
        }
        return indexPop == popV.length;
    }

    public static void main(String[] args) {
        int[] arr1 = {1,2,3,4,5};
        int[] arr2 = {4,5,3,2,1};
        IsPopOrder(arr1, arr2);
    }
}
